#!/bin/sh -e

# Copied from wireshark-common and modified

. /usr/share/debconf/confmodule
db_get kismet/install-setuid
PROGRAMS="/usr/bin/kismet_cap_linux_wifi /usr/bin/kismet_cap_linux_bluetooth /usr/bin/kismet_cap_nrf_mousejack"
GROUP=kismet

for PROGRAM in $PROGRAMS; do
    if [ -e "$PROGRAM" ]; then
        if [ "$RET" = "false" ] ; then
    		 echo "Not installing Kismet with suid-root capture helper.  Kismet "
    		 echo "must be run with 'sudo'.  This is not the preferred nor most secure way "
    		 echo "to install Kismet."
    
             if [ -x /usr/sbin/dpkg-statoverride ] && \
                 ! /usr/sbin/dpkg-statoverride --list $PROGRAM >/dev/null; then
                 chown root:root $PROGRAM
                 chmod u=rwx,go=rx $PROGRAM
             fi
        else
            if [ -x /usr/sbin/dpkg-statoverride ] && \
                ! /usr/sbin/dpkg-statoverride --list $PROGRAM >/dev/null; then
    
                addgroup --quiet --system $GROUP
    
    			db_get kismet/install-users
    
    			if [ "$RET" != "" ]; then
    				for x in ${RET}; do
    					usermod -a -G $GROUP $x
    				done
    			fi
    
                chown root:$GROUP $PROGRAM
    
                if which setcap > /dev/null ; then
    				echo "Installing $PROGRAM capture helper and setting capabilities..."
    
                    chmod u=srwx,g=rx,o=r $PROGRAM
                    if ! setcap cap_net_raw,cap_net_admin=eip $PROGRAM; then
                        echo "Setting capabilities for $PROGRAM using Linux Capabilities failed."
                        echo "Falling back to setting set-user-id bit."
                        chmod u=rwxs,g=rx,o=r $PROGRAM
                    fi
                else
    				echo "Installing Kismet with suid-root $PROGRAM capture helper..."
                    chmod u=rwxs,g=rx,o=r $PROGRAM
                fi
            fi
        fi
    fi
done

